FileIn "Exportador.ms"

fn UABgetProperty Obj Property Default =
(
	if ((getUserProp Obj Property) != undefined) then
	(
		return (getUserProp Obj Property)
	)else
	(
			return Default
	)
)
fn UABGetSelectionString Obj Property Default Down =
(	
	local aux = UABgetProperty Obj Property Default
	for i=1 to Down.items.count do
	(
		if (aux == Down.items[i]) then
		(
			Down.selection = i
			break
		)
	)
)

fn StaticMeshManager Cores PathFile LevelName ExportMeshes CopyTextures = 
(
	makedir (PathFile + "data\\") all:true
	FileName = PathFile + "data\\static_meshes.xml"
	local file=createfile FileName
	format "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" to:file
	format "<static_meshes>\n" to:file
	--TODO acceder a los diferentes hijos
	for i=1 to Cores.count do
	(
		format "\t<static_mesh name=\"%\" filename=\"%\"/>\n" Cores[i].name ("./data/static_meshes/"+LevelName+"/"+Cores[i].name+".mesh") to:file
	)
	format "</static_meshes>\n" to:file
	fflush file
	close file
	if (ExportMeshes) then
	(		
			local PathFinal = PathFile + "Data\\"
			ExportMeshPressed Cores PathFinal LevelName CopyTextures
	)
)

fn ExportMeshPressed Objs PathFile LevelName CopyTextures =
(
		for i=1 to Objs.count do
		(
			if ((UABgetProperty Objs[i] "Export_Type" "a") == "Instance_Mesh") then
			(
				ExportMesh Objs[i] PathFile LevelName CopyTextures
			)
		)
)


function RenderableObjectManager PathFile Cores InstBycore Instances=
(
	-- TODO path con data/level/etc?
	FileName = Pathfile + "Data\\Renderable_Objects.xml"
	local file = createfile FileName
	format "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" to:file
	format "<renderable_objects>\n" to:file

	for i=1 to Instances.count do
	(
		format "\t<mesh_instance  Layer=\"%\"  Name=\"%\"  Core=\"%\"  Pos=\"% % %\"  Yaw=\"%\"  Pitch=\"%\"  Roll=\"%\" Scale= \"%\" />\n" (UABgetProperty $ "LayerType" "False") Instances[i].name InstByCore[i].name Instances[i].position.x Instances[i].position.y Instances[i].position.z (GetYaw Instances[i].transform) (GetPitch Instances[i].transform) (GetRoll Instances[i].transform) (RHTranslationToLH Instances[i].transform.scale) to:file
	)--fin for

	format "</renderable_objects>" to:file
	fflush file
	close file
)

fn GetCoreInstance Objs Cores InstancesByCores Instances = 
(
	for i=1 to Objs.count do
	(
		if ((UABgetProperty objs[i] "Export_Type" "a") == "Instance_Mesh") then
		(	
			local Obj=Objs[i]
			local core=undefined
			for j=1 to  Cores.count do
			(
				if (areNodesInstances Obj Cores[j]) then
				(
					core=Cores[j]
					break
				)
			)
			if core==undefined then
			(
				append Cores Obj
				append InstancesByCores Obj
				append Instances Obj
			)
			else
			(
				append InstancesByCores Core
				append Instances Obj
			)
		)
	)
)
/* DEBUG:
global Cores=#()
global InstancesByCores=#()
global Instances=#()
GetCoreInstance $* Cores InstancesByCores Instances
StaticMeshManager Cores "D:\\" "Level1" true true
RenderableObjectManager "D:\\" Cores InstancesByCores Instances
*/
